<?php

class CustomerController extends BackendApplicationController {

	public $layout = 'application.modules.delivery.views.layouts.delivery';
	public function actionView($id) {
		$this->render('view', array(
			'model' => $this->loadModel($id, 'DeliveryCustomerModel'),
		));
	}

	public function actionCreate() {
		$model = new DeliveryCustomerModel;


		if (isset($_POST['DeliveryCustomerModel'])) {
			$model->setAttributes($_POST['DeliveryCustomerModel']);
			$model->setAttribute('created_datetime', date('Y-m-d H:i:s'));
			$model->setAttribute('code', 'AM');
			if ($model->save()) {
				$update = $model->setAttribute('code', 'AM'.$model->id);
				$model->save();
				if (Yii::app()->getRequest()->getIsAjaxRequest())
					Yii::app()->end();
				else
					$this->redirect(array('view', 'id' => $model->id));
			}
		}

		$this->render('create', array( 'model' => $model));
	}

	public function actionUpdate($id) {
		$model = $this->loadModel($id, 'DeliveryCustomerModel');


		if (isset($_POST['DeliveryCustomerModel'])) {
			$model->setAttributes($_POST['DeliveryCustomerModel']);

			if ($model->save()) {
				$this->redirect(array('view', 'id' => $model->id));
			}
		}

		$this->render('update', array(
				'model' => $model,
				));
	}

	public function actionDelete($id) {
		if (Yii::app()->getRequest()->getIsPostRequest()) {
			$this->loadModel($id, 'DeliveryCustomerModel')->delete();

			if (!Yii::app()->getRequest()->getIsAjaxRequest())
				$this->redirect(array('admin'));
		} else
			throw new CHttpException(400, Yii::t('app', 'Your request is invalid.'));
	}

	public function actionIndex() {
		$dataProvider = new CActiveDataProvider('DeliveryCustomerModel');
		$this->render('index', array(
			'dataProvider' => $dataProvider,
		));
	}

	public function actionAdmin() {
		$model = new DeliveryCustomerModel('search');
		$model->unsetAttributes();

		if (isset($_GET['DeliveryCustomerModel']))
			$model->setAttributes($_GET['DeliveryCustomerModel']);

		$this->render('admin', array(
			'model' => $model,
		));
	}
	public function actionListChoice()
	{
		$this->layout = 'application.modules.menu.views.layouts.ajax';
		$model = new DeliveryCustomerModel('search');
		$model->unsetAttributes();
		
		if (isset($_GET['DeliveryCustomerModel']))
			$model->attributes = $_GET['DeliveryCustomerModel'];
		$this->render('list', array(
				'model' => $model,
		));
	}
	public function actionGetCus($id)
	{
		$data = DeliveryCustomerModel::model()->findByPk($id);
		$result['id'] = $data->id;
		$result['name'] = $data->name;
		$result['company'] = $data->company;
		$result['address'] = $data->address;
		$result['phone'] = $data->phone;
		echo CJSON::encode($result);
		$this->layout=false;
		Yii::app()->end();
	}

}